package cn.iocoder.yudao.module.eval.controller.admin.certificationscheme;

import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.eval.controller.admin.certificationscheme.vo.*;
import cn.iocoder.yudao.module.eval.dal.dataobject.certificationscheme.CertificationSchemeDO;
import cn.iocoder.yudao.module.eval.service.certificationscheme.CertificationSchemeService;

@Tag(name = "管理后台 - 认定方案表")
@RestController
@RequestMapping("/eval/certification-scheme")
@Validated
public class CertificationSchemeController {

    @Resource
    private CertificationSchemeService certificationSchemeService;

    @PostMapping("/create")
    @Operation(summary = "创建认定方案表")
    @PreAuthorize("@ss.hasPermission('eval:certification-scheme:create')")
    public CommonResult<Long> createCertificationScheme(@Valid @RequestBody CertificationSchemeSaveReqVO createReqVO) {
        return success(certificationSchemeService.createCertificationScheme(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新认定方案表")
    @PreAuthorize("@ss.hasPermission('eval:certification-scheme:update')")
    public CommonResult<Boolean> updateCertificationScheme(@Valid @RequestBody CertificationSchemeSaveReqVO updateReqVO) {
        certificationSchemeService.updateCertificationScheme(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除认定方案表")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('eval:certification-scheme:delete')")
    public CommonResult<Boolean> deleteCertificationScheme(@RequestParam("id") Long id) {
        certificationSchemeService.deleteCertificationScheme(id);
        return success(true);
    }

    @DeleteMapping("/delete-list")
    @Parameter(name = "ids", description = "编号", required = true)
    @Operation(summary = "批量删除认定方案表")
                @PreAuthorize("@ss.hasPermission('eval:certification-scheme:delete')")
    public CommonResult<Boolean> deleteCertificationSchemeList(@RequestParam("ids") List<Long> ids) {
        certificationSchemeService.deleteCertificationSchemeListByIds(ids);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得认定方案表")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('eval:certification-scheme:query')")
    public CommonResult<CertificationSchemeRespVO> getCertificationScheme(@RequestParam("id") Long id) {
        CertificationSchemeDO certificationScheme = certificationSchemeService.getCertificationScheme(id);
        return success(BeanUtils.toBean(certificationScheme, CertificationSchemeRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得认定方案表分页")
    @PreAuthorize("@ss.hasPermission('eval:certification-scheme:query')")
    public CommonResult<PageResult<CertificationSchemeRespVO>> getCertificationSchemePage(@Valid CertificationSchemePageReqVO pageReqVO) {
        PageResult<CertificationSchemeDO> pageResult = certificationSchemeService.getCertificationSchemePage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, CertificationSchemeRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出认定方案表 Excel")
    @PreAuthorize("@ss.hasPermission('eval:certification-scheme:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportCertificationSchemeExcel(@Valid CertificationSchemePageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<CertificationSchemeDO> list = certificationSchemeService.getCertificationSchemePage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "认定方案表.xls", "数据", CertificationSchemeRespVO.class,
                        BeanUtils.toBean(list, CertificationSchemeRespVO.class));
    }

}